<template>
  <div class="login-wrap">
    <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px"
             class="demo-ruleForm login-container">
      <h3 class="title">用 户 注 册</h3>
      <el-form-item align="center" prop="userAccount">
        <el-input type="text" style="width:80%;" v-model="ruleForm.userAccount" auto-complete="off"
                  placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item align="center" prop="userPassword">
        <el-input type="password" style="width:80%;" v-model="ruleForm.userPassword" auto-complete="off" show-password
                  placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item align="center" prop="userPassword1">
        <el-input type="password" style="width:80%;" v-model="ruleForm.userPassword1" auto-complete="off" show-password
                  placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item align="center" prop="userName">
        <el-input type="text" style="width:80%;" v-model="ruleForm.userName" auto-complete="off"
                  placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item align="center" prop="sex">
        <el-select v-model="ruleForm.sex" placeholder="请选择性别" style="width: 80%;">
          <el-option key="1" label="男" value="1"></el-option>
          <el-option key="2" label="女" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" prop="age">
        <el-input type="text" style="width:80%;" v-model="ruleForm.age" auto-complete="off"
                  placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item align="center" prop="phoneNumber">
        <el-input type="text" style="width:80%;" v-model="ruleForm.phoneNumber" auto-complete="off"
                  placeholder="电话号码"></el-input>
      </el-form-item>
      <el-form-item align="center" style="width:100%;text-align:center;">
        <el-button type="primary" style="width:80%;background-color:#a11a1a;border:none;" @click="submitForm"
                   :loading="logining">注册
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as api from '../api/interface'
import {register} from "../api/interface";

export default {
  data() {
    return {
      //定义loading默认为false
      logining: false,
      //输入的数据--与实体属性名相同
      ruleForm: {
        userAccount: '',
        userPassword: ''
      },
      user: {},
      //rules前端验证，当输入框为空时
      rules: {
        userAccount: [{required: true, message: '请输入账号', trigger: 'blur'}],
        userPassword: [{required: true, message: '请输入密码', trigger: 'blur'}],
        userPassword1: [{required: true, message: '请再次输入密码', trigger: 'blur'}],
        userName: [{required: true, message: '请输入姓名', trigger: 'blur'}],
        sex: [{required: true, message: '请选择性别', trigger: 'blur'}],
        age: [{required: true, message: '请输入年龄', trigger: 'blur'}],
        phoneNumber: [{required: true, message: '请输入电话号码', trigger: 'blur'}],
      }
    }
  },
  // 页面初始化操作
  created() {
    console.log("init success");
  },
  // 里面的函数只有调用才会执行
  methods: {
    submitForm(formName) {
      //对ref="ruleForm"进行表单验证的规则
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.logining = true
          api.register(this.ruleForm).then(data => {
            this.logining = false;
            if (data.message != "success") {
              //查询不成功时设置，error为红色
              this.logining = false;
              this.$message.error(data.message)
            } else {
              this.$message.success("注册成功")
              this.$router.push({
                //前端路径
                path: '/login'
              })
            }
          }).catch((error, status) => {
            this.logining = false;
          });
        } else {
          this.$message.error('请输入用户名密码！')
          this.logining = false;
          return false;
        }
      })
    },
    register() {
      this.$router.push({ path: '/system/register' })
    },
  }
}
</script>

<style scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1a2a6c 0%, #b21f1f 51%, #fdbb2d 100%);
}

.login-container {
  width: 500px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.title {
  margin-bottom: 40px;
  font-size: 24px;
  font-weight: 600;
  color: #1a2a6c;
  text-align: center;
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }

  .el-input {
    height: 42px;

    /deep/ .el-input__inner {
      height: 42px;
      line-height: 42px;
      border-radius: 4px;
      border: 1px solid #dcdfe6;

      &:focus {
        border-color: #b21f1f;
        box-shadow: 0 0 0 2px rgba(178, 31, 31, 0.1);
      }
    }
  }
}

.verify-code-container {
  display: flex;
  gap: 10px;
}

.verify-input {
  flex: 1;
}

.verify-code {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 42px;
  background: linear-gradient(45deg, #1a2a6c, #b21f1f);
  color: white;
  font-family: 'Arial', sans-serif;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 2px;
  cursor: pointer;
  user-select: none;
  border-radius: 4px;
  transition: transform 0.3s ease;
}

.verify-code:hover {
  transform: scale(1.02);
}

.login-button {
  width: 100%;
  height: 42px;
  font-size: 16px;
  background: #b21f1f;
  border: none;
  border-radius: 4px;
  transition: all 0.3s ease;

  &:hover {
    background: #d42525;
    transform: translateY(-1px);
  }

  &:active {
    transform: translateY(1px);
  }
}

.login-footer {
  margin-top: 20px;
  text-align: center;
}

.register-link {
  color: #b21f1f;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s ease;

  &:hover {
    color: #d42525;
    text-decoration: underline;
  }
}

@media screen and (max-width: 480px) {
  .login-container {
    width: 90%;
    padding: 30px 20px;
  }

  .title {
    font-size: 20px;
    margin-bottom: 30px;
  }

  .verify-code {
    width: 80px;
  }
}

.el-input__inner:focus {
  transition: all 0.3s ease;
}

.el-button.is-loading {
  position: relative;
  pointer-events: none;
  opacity: 0.8;
}
</style>
